package back.controller;

import back.pojo.Book;
import back.pojo.R;
import back.pojo.User;
import back.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin/book")
public class BookController {
    @Autowired
    private BookService bookService;
    private String msg="请求成功";
    /**
     * 处理用户访问错误的路径
     * @return
     */
    @RequestMapping("/**")
    public String error(){
        return "error/404";
    }

    /**
     * 跳转书籍管理页面
     * @return
     */
    @GetMapping("/index")
    public String book() {
        return "/admin/book_add";
    }

    /**
     * 查询书籍
     * @param book 1、如果属性为空 查询所有数据；2、如果ISBN不为空 查询ISBN的书籍
     * @return
     */
    @PostMapping("/search")
    @ResponseBody
    public R search(Book book) {
        System.out.println("book = " + book);
        R r = new R();
        // 查询用户所有的数量
        int total = bookService.getCountBook(book);
//        System.out.println("book.getPageSize() = " + book.getPageSize());
        int pageSize = 10;
        if (book.getPageSize() != null && book.getPageSize() != 0) {
            pageSize = book.getPageSize();
        }
        int pages = total / pageSize + (total % pageSize == 0 ? 0 : 1);
        // 查询的当前页
        book.updateSkip();
        List<Book> bookList = bookService.queryBookList(book);

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("total", total);
        map.put("pages", pages);
        map.put("rows", bookList);

        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);

        return r;
    }

    /**
     * 通过id查询指定的书籍
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public R queryBookById(@PathVariable int id) {
        R r = new R();
        Book book = bookService.queryBookById(id);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("data", book);

        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);
        return r;
    }

    /**
     * 添加书籍
     * @param book 前端填写的表单数据
     * @return
     */
    @PostMapping("/addBook")
    @ResponseBody
    public R addBook(Book book) {
        R r = new R();
        HashMap<String, Object> map = new HashMap<String, Object>();
/*        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(book.getPubDate());
        System.out.println("book = " + format);*/

        List<Book> books = bookService.queryBookList(new Book());
        for (Book book1 : books) {
            if (!book1.getISBN().equals(book.getISBN())){
                bookService.addBook(book);
                map.put("data", book);
                r.setCode(200);
                r.setMsg(msg);
                r.setData(map);
                break;
            }else {
                r.setCode(401);
                r.setMsg("已存在ISBN书籍");
                r.setData(null);
                break;
            }
        }
        return r;
    }

    /**
     * 修改书籍信息
     * @param book 前段传入修改后的数据
     * @return
     */
    @PostMapping("/updateBook")
    @ResponseBody
    public R updateBook(Book book) {
        R r = new R();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("data", book);
        System.out.println("book = " + book);
        bookService.updateBook(book);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);

        return r;
    }

    /**
     * 通过id 删除自指定的书籍
     * @param id
     * @return
     */
    @PostMapping("/del/{id}")
    @ResponseBody
    public R deleteBookById(@PathVariable Integer id){
        R r = new R();
        HashMap<String, Object> map = new HashMap<String, Object>();

            bookService.deleteBookById(id);
            r.setCode(200);
            r.setMsg(msg);
            r.setData(map);
        return r;
    }

    /**
     * 通过多个id 批量删除指定书籍
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public R deleteBook(@RequestBody List<Integer> ids){
        R r = new R();
        HashMap<String, Object> map = new HashMap<String, Object>();

        bookService.deleteBook(ids);

        map.put("data",ids);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);

        return r;
    }
}
